package cn.itcast.demo;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;

public class LuceneIndexDeleteTest {
    @Test
    public void testDeleteIndex() throws IOException {

        Directory directory = FSDirectory.open(new File("indexDir"));
        Analyzer analyzer = new IKAnalyzer();
        IndexWriterConfig conf = new IndexWriterConfig(Version.LATEST,analyzer);
        IndexWriter indexWriter = new IndexWriter(directory,conf);
//        删除所有
//        indexWriter.deleteAll();
//        indexWriter.deleteDocuments(new Term("id","1"));
        Query query = NumericRangeQuery.newLongRange("id",1L,1L,true,true);
        //传入query或者term都表示查询条件，（把查询条件匹配到的数据全部删除）
        indexWriter.deleteDocuments(query);
        indexWriter.commit();
        indexWriter.close();
    }
}
